import net.zjitc.pojo.User;
import net.zjitc.pojo.UserVo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/11 9:01
 */
public class myBatis {
    SqlSessionFactory inputStream=null;
    private SqlSessionFactory factory;

    @Before
    public void createFactory(){
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream("myBatis.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void testSession(){

            SqlSession session = factory.openSession();
            User user = session.selectOne("userTest.findById",1);
            System.out.println(user);
    }
    @Test
    public void testDelete(){
        SqlSession session = factory.openSession();
        int num = session.delete("userTest.delById", 4);
        session.commit();
        System.out.println(num);
    }
    @Test
    public void testFindAll(){
        SqlSession session = factory.openSession();
        List<User> user = session.selectList("userTest.findAll");
        System.out.println(user);
    }
    @Test
    public void testAdd(){
        UserVo user = new UserVo();
        user.setUsername("雅玖");
        user.setPassword("123456");
        SqlSession session = factory.openSession();
        int insert = session.insert("userTest.addById", user);
        session.commit();
        System.out.println(insert);
    }
    @Test
    public void testUpdate(){

    }
}
